package pub.developers.forum.facade.impl;

import org.springframework.stereotype.Service;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.api.request.file.FileUploadImgRequest;
import pub.developers.forum.api.service.FileApiService;
import pub.developers.forum.app.manager.FileManager;
import pub.developers.forum.common.enums.ErrorCodeEn;
import pub.developers.forum.facade.support.ResultModelUtil;
import pub.developers.forum.facade.validator.FileValidator;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Objects;


@Service
public class FileApiServiceImpl implements FileApiService {

    @Resource
    private FileManager fileManager;


    //七牛云
    @Override
    public ResultModel<String> uploadImg(FileUploadImgRequest request) {
        FileValidator.uploadImg(request);

        return ResultModelUtil.success(fileManager.uploadImg(request));
    }

    //删除七牛云上指定的照片
    @Override
    public ResultModel deleteImg(String avatarName) {
        //获取照片的Key
        if (Objects.equals(fileManager.deleteAvatar(avatarName), "error"))
        {
            return ResultModelUtil.fail(ErrorCodeEn.SYSTEM_ERROR);
        }
        return ResultModelUtil.success();
    }

    @Override
    public ResultModel<String> uploadImgOss(FileUploadImgRequest request) throws ParseException {
        FileValidator.uploadImg(request);
        String information = fileManager.uploadImgOss(request);
        if (information.equals("error"))
        {
            return ResultModelUtil.fail(ErrorCodeEn.IMAGE_IS_NOPASS);
        }
        return ResultModelUtil.success(information);
    }

    //删除Oss上指定的照片
    @Override
    public ResultModel deleteImgOss(String avatarName) {
        //获取照片的Key
        if (Objects.equals(fileManager.deleteAvatarOss(avatarName), "error"))
        {
            return ResultModelUtil.fail(ErrorCodeEn.SYSTEM_ERROR);
        }
        return ResultModelUtil.success();
    }
}
